﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualTournaments.Business.Tournaments
{
    public class TeamStatics : IComparable
    {
        Team _team;
        int _points;
        int _goalsFor;
        int _goalsAgainst;
        int _goalsDifference;
        int _playedMatches;
        int _wonMatches;
        int _drawMatches;
        int _lostMatches;

        private const int WON_POINTS = 3;
        private const int DRAW_POINTS = 1;
        private const int LOST_POINTS = 0;

        public TeamStatics(Team team)
        {
            this.Team = team;
            Points = 0;
            GoalsFor = 0;
            GoalsAgainst = 0;
            GoalsDifference = 0;
            PlayedMatches = 0;
            WonMatches = 0;
            DrawMatches = 0;
            LostMatches = 0;
        }

        public Team Team
        {
            get { return _team; }
            protected set { _team = value; }
        }

        public int Points
        {
            get { return _points; }
			protected set { _points = value; }
        }

        public int GoalsFor
        {
            get { return _goalsFor; }
			protected set { _goalsFor = value; }
        }

        public int GoalsAgainst
        {
            get { return _goalsAgainst; }
			protected set { _goalsAgainst = value; }
        }

        public int GoalsDifference
        {
            get { return _goalsDifference; }
			protected set { _goalsDifference = value; }
        }

        public int PlayedMatches
        {
            get { return _playedMatches; }
			protected set { _playedMatches = value; }
        }

        public int WonMatches
        {
            get { return _wonMatches; }
            protected set { _wonMatches = value; }
        }

        public int DrawMatches
        {
            get { return _drawMatches; }
            protected set { _drawMatches = value; }
        }

        public int LostMatches
        {
            get { return _lostMatches; }
			protected set { _lostMatches = value; }
        }

        public void RegisterMatch(int goalsFor, int goalsAgainst)
        {
            ++PlayedMatches;
            GoalsFor += goalsFor;
            GoalsAgainst += goalsAgainst;
            GoalsDifference += (goalsFor - goalsAgainst);
            if (goalsFor > goalsAgainst)
            {
                ++WonMatches;
                Points += WON_POINTS;
            }
            else if (goalsAgainst > goalsFor)
            {
                ++LostMatches;
                Points += LOST_POINTS;
            }
            else
            {
                ++DrawMatches;
                Points += DRAW_POINTS;
            }
        }

        public override String ToString()
        {
            return String.Concat(Team.Name," ",Points," ",PlayedMatches," ",WonMatches," ",DrawMatches," ",LostMatches," ",GoalsFor," ",GoalsAgainst," ",GoalsDifference); 
        }

        public int CompareTo(object teamStatics)
        {
            TeamStatics otherTeamStatics = (TeamStatics) teamStatics;
            if (Points < otherTeamStatics.Points)
                return 1;
            else if (Points > otherTeamStatics.Points)
                return -1;
            else if (GoalsDifference < otherTeamStatics.GoalsDifference)
                return 1;
            else if (GoalsDifference > otherTeamStatics.GoalsDifference)
                return -1;
            else
                return 0;
        }

    }
}
